<?php
require_once('classDB.php');
class tin extends db{

	//---------------------------------------NEW TOUR---------------------------------------//
	public function MotTinMoiNhat($lang='vi'){
		$sql="	SELECT idTin, TieuDe, TomTat, tin.Content, urlHinh, Ngay, SoLanXem, tin.AnHien, TinNoiBat, TenTL, Ten  
			FROM  tin, theloai, loaitin
			WHERE tin.idTL=theloai.idTL 
			AND tin.idLT = loaitin.idLT
			AND tin.AnHien=1 AND tin.TinNoiBat=1
			AND ( tin.lang='$lang' or '$lang'='' )
			ORDER BY tin.idTin DESC
			LIMIT 0,2";
		$kq = mysql_query($sql) or die (mysql_error());	
		return $kq;
	}//end function TourMoiNhat
	
	public function TinMoiNhat($lang='vi'){
		$sql="	SELECT idTin, TieuDe, TomTat, tin.Content, urlHinh, Ngay, SoLanXem, tin.AnHien, tin.TinNoiBat, TenTL, Ten  
			FROM  tin, theloai, loaitin
			WHERE tin.idTL=theloai.idTL 
			AND tin.idLT = loaitin.idLT
			AND tin.AnHien=1 AND tin.TinNoiBat=0
			AND ( tin.lang='$lang' or '$lang'='' )
			AND tin.idLT<>99 and tin.idLT<>100 and tin.idLT<>101 AND tin.idTL<>22 AND tin.idTL<>26
			ORDER BY tin.idTin DESC
			LIMIT 0,4";
		$kq = mysql_query($sql) or die (mysql_error());	
		return $kq;
	}//end function TourMoiNhat
	//---------------------------------------NEW TOUR---------------------------------------//
	public function TinXemNhieu($lang = 'vi', $sotin = 8){		
		$sql="SELECT idTin, TieuDe, TomTat, urlHinh, Ngay, SoLanXem
			FROM tin WHERE AnHien = 1 and ( lang = '$lang' or '$lang' = '' ) 
			ORDER BY SoLanXem DESC 	LIMIT 0,$sotin";
		$kq = mysql_query($sql) or die (mysql_error());		
		return $kq;		
	}//end function TinXemNhieu
	
	//---------------------------------------QUANG CAO---------------------------------------//
	public function QuangCao_Top($lang='vi', $sotin=5){
		$sql = "SELECT idQC, TenQC, MoTa, urlHinh, urlLink
				FROM quangcao_top
				WHERE AnHien=1 AND Ngay DESC AND ( lang = '$lang' or '$lang' = '' )
				LIMIT 0,$sotin";
		$kq = mysql_query($sql) or die(mysql_error($sql));
		return $kq;
	}//end function QuangCao_Top
	//---------------------------------------QUANG CAO---------------------------------------//
	
	//---------------------------------------TIM KIEM---------------------------------------//
	/*public function TimKiem($lang='vi', $tukhoa){
		$sql = "SELECT * FROM tin 
				WHERE TieuDe LIKE '%$tukhoa%' 
				AND ( lang = '$lang' or '$lang' = '' ) 
				ORDER BY idTin DESC";
		$kq = mysql_query($sql);
		return $kq;
	}//end function TimKiem
	*/
	//---------------------------------------TIM KIEM---------------------------------------//

	function ChiTietTin($idTin)  {
		settype($idTin, "int");
		$sql="SELECT idTin, TieuDe, TomTat, Ngay, urlHinh, Content, SoLanXem, 
			TinNoiBat, tin.AnHien, tin.idLT, Ten, tin.idTL, TenTL, loaitin.idLT as idloaitin, theloai.idTL as idtheloai
			FROM  tin, loaitin, theloai
			WHERE tin.idLT=loaitin.idLT  AND loaitin.idTL=theloai.idTL AND idTin=$idTin";
		$kq = mysql_query($sql) or die(mysql_error());
		return $kq;
	}//end function ChiTietTin

	function Welcome($lang='vi', $AnHien=1){
		$sql="SELECT * from welcome WHERE (AnHien=$AnHien or $AnHien=-1)
				AND (lang = '$lang' or '$lang'='') 
				ORDER BY ThuTu
				LIMIT 0,1";
		$kq = mysql_query($sql) or die(mysql_error());
		return $kq;
	}//Welcome
	
	function Travel($lang='vi', $AnHien=1){
		$sql="SELECT * from travel_tip WHERE (AnHien=$AnHien or $AnHien=-1)
				AND (lang = '$lang' or '$lang'='') 
				ORDER BY ThuTu
				LIMIT 0,1";
		$kq = mysql_query($sql) or die(mysql_error());
		return $kq;
	}//end function Travel
	
	function TopTour($lang='vi', $AnHien=1){
		$sql="SELECT idTin, TieuDe from tin WHERE (AnHien=$AnHien or $AnHien=-1)
				AND (lang = '$lang' or '$lang'='') AND TinNoiBat=1
				ORDER BY Ngay DESC
				LIMIT 0,5";
		$kq = mysql_query($sql) or die(mysql_error());
		return $kq;
	}//end function TopTour
	
	function CapNhatSolanXemTin($idTin){
		settype($idTin,"int");
		$sql = "UPDATE tin SET SolanXem = SoLanXem+1 WHERE idTin = $idTin";
		mysql_query($sql) or die(mysql_error());
	}//end function CapNhatSolanXemTin
	
	//---------------------------------------TIM CU-MOI-CUNG SU KIEN---------------------------------------//
	function TinCuCungLoai($idTin, $lang ='vi', $sotin = 5){		
		settype($idTin, "int");
		$sql="SELECT idTin, TieuDe, TomTat, urlHinh, Ngay, SoLanXem  FROM  tin 
			WHERE AnHien = 1 AND idTin<$idTin AND ( lang ='$lang' or '$lang'='' )
			AND idLT = (SELECT idLT FROM tin WHERE idTin = $idTin)
			ORDER BY idTin DESC  LIMIT 0, $sotin";		
		$kq = mysql_query($sql) or die(mysql_error());
		return $kq;		
	}//end function TinCuCungLoai
	
	function TinMoiCungLoai($idTin, $lang ='vi', $sotin = 5){		
		settype($idTin, "int");
		$sql="SELECT idTin, TieuDe, TomTat, urlHinh, Ngay, SoLanXem  FROM  tin 
			WHERE AnHien = 1 AND idTin>$idTin AND ( lang ='$lang' or '$lang'='' )
			AND idLT = (SELECT idLT FROM tin WHERE idTin = $idTin)
			ORDER BY idTin DESC  LIMIT 0, $sotin";		
		$kq = mysql_query($sql) or die(mysql_error());
		return $kq;		
	}//end function TinMoiCungLoai
	
	function CungSuKienVoiTin($idTin, $lang ='vi', $sotin = 5 ){		
		settype($idTin, "int");
		$sql="SELECT idTin, TieuDe, TomTat, urlHinh, Ngay, SoLanXem  FROM   tin 
			WHERE  AnHien = 1 AND ( lang ='{$lang}'  or '{$lang}'='' )
			AND idSK = (SELECT idSK FROM tin WHERE idTin=$idTin)
			LIMIT 0, $sotin";
		$kq = mysql_query($sql) or die(mysql_error());
		return $kq;		
	}//end function CungSuKienVoiTin
	//---------------------------------------TIM CU-MOI-CUNG SU KIEN---------------------------------------//
		
	//---------------------------------------CAC TIN TRONG LOAI---------------------------------------//
	function TinTrongLoai($idLT, $lang ='vi', $pageNum = 1, $pageSize = 10){		
		$startRow = ($pageNum-1)*$pageSize;
		$sql="SELECT idTin, TieuDe, TomTat, urlHinh, Ngay, SoLanXem
			FROM  tin WHERE AnHien = 1 AND idLT=$idLT AND ( lang ='$lang' or '$lang'='' )
			ORDER BY idTin DESC  LIMIT $startRow , $pageSize";
		$kq = mysql_query($sql) or die(mysql_error());	
		return $kq;		
	}//end function TinTrongLoai
	
	function TinTrongLoai2($idLT, $lang ='vi'){	
		settype($idLT, "int");	
		$sql="SELECT idTin, TieuDe, TomTat, urlHinh, Ngay, SoLanXem
			FROM  tin WHERE AnHien = 1 AND idLT=$idLT AND ( lang ='{$lang}'  or '{$lang}'='' )
			ORDER BY idTin DESC  LIMIT 0,8";
		$kq = mysql_query($sql) or die(mysql_error());	
		return $kq;		
	}//end function TinTrongLoai
	
	function ChiTietLoaiTin($idLT) {
		settype($idLT, "int");
		$sql="SELECT idLT, Ten, loaitin.idTL, TenTL, theloai.idTL
			FROM loaitin, theloai
			WHERE loaitin.idTL = theloai.idTL AND idLT = $idLT";		
		$kq = mysql_query($sql) or die(mysql_error());
		return $kq;		
	}//end function ChiTietLoaiTin
	//---------------------------------------CAC TIN TRONG LOAI---------------------------------------//
	
	//---------------------------------------GALLERY---------------------------------------//
	public function ListHinhAnh($idDD=-1, $idHinh=-1, $AnHien = -1){
		$sql = "SELECT idHinh, ChuThich, hinhanh.Ten AS TenHinh, urlHinh,urlThum,  hinhanh.idDiadanh AS idDD, Ngay, hinhanh.AnHien, diadanh.Ten AS TenDiaDanh FROM hinhanh, diadanh
				WHERE (idHinh=$idHinh OR $idHinh=-1) AND (hinhanh.AnHien=$AnHien OR $AnHien=-1) AND (hinhanh.idDiadanh=$idDD OR $idDD=-1)
				AND hinhanh.idDiadanh = diadanh.idDiadanh
				ORDER BY Rand()
				LIMIT 0,7" ;
		$kq = mysql_query($sql) or die(mysql_error());
		return $kq;
	}//end function ListHinhAnh
	//---------------------------------------GALLERY---------------------------------------//
	
	//---------------------------------------SEARCH---------------------------------------//
	function TimKiem($tukhoa,$idLT,$tungay,$denngay,$pageNum=1,$pageSize=10,$lang='vi', &$totalRows){
		$tukhoa = trim(strip_tags($tukhoa));
		$tungay = trim(strip_tags($tungay));
		$denngay = trim(strip_tags($denngay));
		settype($idLT,"int");
		if (get_magic_quotes_gpc()==false) {
			$tukhoa = mysql_real_escape_string($tukhoa);
			$tungay = mysql_real_escape_string($tungay);
			$denngay = mysql_real_escape_string($denngay);		
		}
		$tungay_arr = explode("/",$tungay); // array(17,11,1990)
		$tn="1970-01-01";
		if (count($tungay_arr)==3) {
			$d = $tungay_arr[0]; //17
			$m = $tungay_arr[1]; //11
			$y = $tungay_arr[2]; //1990
			if (checkdate($m,$d,$y)==true) $tn = $y."-".$m."-".$d;
		}
		$denngay_arr = explode("/",$denngay); // array(17,11,1990)
		$dn = date("Y-m-d");
		if (count($denngay_arr)==3) {
			$d = $denngay_arr[0]; //17
			$m = $denngay_arr[1]; //11
			$y = $denngay_arr[2]; //1990
			if (checkdate($m,$d,$y)==true) $dn = $y."-".$m."-".$d;
		}	
		$totalRows = 0;
		$sql = "SELECT count(*) 
			FROM tin, loaitin, theloai
			WHERE tin.AnHien = 1 AND tin.idLT = loaitin.idLT AND tin.idTL = theloai.idTL AND (TieuDe RegExp '$tukhoa' or TomTat RegExp '$tukhoa') AND
			(tin.idLT = $idLT or $idLT=-1) AND  (Ngay>='$tn' AND Ngay<='$dn' ) AND 
			(tin.lang='$lang' or '$lang'='') 
			ORDER BY idTin DESC";
		$kq = mysql_query($sql,$this->conn) or die (mysql_error());	
		$row_kq = mysql_fetch_row($kq);
		$totalRows = $row_kq[0];
			
		$startRow = ($pageNum - 1)*$pageSize;
		$sql = "SELECT idTin, TieuDe, TomTat, urlHinh, Ngay, SoLanXem, Ten, TenTL
			FROM tin, loaitin, theloai
			WHERE ( 
				tin.AnHien = 1 AND tin.idLT = loaitin.idLT AND tin.idTL = theloai.idTL 
				AND (TieuDe RegExp '$tukhoa' or TomTat RegExp '$tukhoa') 
				AND(tin.idLT = $idLT or $idLT=-1) AND  (Ngay>='$tn' AND Ngay<='$dn' ) 
				AND (tin.lang='$lang' or '$lang'='') 
			)
			ORDER BY idTin DESC LIMIT $startRow , $pageSize";		
		$kq = mysql_query($sql) or die (mysql_error());
		return $kq;
	}
	
	function TimKiem2($tukhoa,$idLT,$tungay,$denngay,$pageNum=1,$pageSize=10,$lang='vi', &$totalRows){
		$tukhoa = trim(strip_tags($tukhoa));
		$tungay = trim(strip_tags($tungay));
		$denngay = trim(strip_tags($denngay));
		settype($idLT,"int");
		if (get_magic_quotes_gpc()==false) {
			$tukhoa = mysql_real_escape_string($tukhoa);
			$tungay = mysql_real_escape_string($tungay);
			$denngay = mysql_real_escape_string($denngay);		
		}
		$tungay_arr = explode("/",$tungay); // array(17,11,1990)
		$tn="1970-01-01";
		if (count($tungay_arr)==3) {
			$d = $tungay_arr[0]; //17
			$m = $tungay_arr[1]; //11
			$y = $tungay_arr[2]; //1990
			if (checkdate($m,$d,$y)==true) $tn = $y."-".$m."-".$d;
		}
		$denngay_arr = explode("/",$denngay); // array(17,11,1990)
		$dn = date("Y-m-d");
		if (count($denngay_arr)==3) {
			$d = $denngay_arr[0]; //17
			$m = $denngay_arr[1]; //11
			$y = $denngay_arr[2]; //1990
			if (checkdate($m,$d,$y)==true) $dn = $y."-".$m."-".$d;
		}	
		$totalRows = 0;
		$sql = "SELECT count(*) 
			FROM customer
			WHERE fullname_no RegExp '$tukhoa'
			ORDER BY idCustomer DESC";
		$kq = mysql_query($sql,$this->conn) or die (mysql_error());	
		$row_kq = mysql_fetch_row($kq);
		$totalRows = $row_kq[0];
			
		$startRow = ($pageNum - 1)*$pageSize;
		$sql = "SELECT *
			FROM customer
			WHERE fullname_no RegExp '$tukhoa'
			ORDER BY idCustomer DESC LIMIT $startRow , $pageSize";		
		$kq = mysql_query($sql) or die (mysql_error());
		return $kq;
	}


	
	//---------------------------------------SEARCH---------------------------------------//
	
	//---------------------------------------TIN NGOAI LE---------------------------------------//
	public function TinTrongNuoc($lang='vi'){
		$sql="	SELECT idTin, TieuDe, TomTat, tin.Content, urlHinh, Ngay, SoLanXem, tin.AnHien, tin.TinNoiBat, TenTL, Ten  
			FROM  tin, theloai, loaitin
			WHERE tin.idTL=theloai.idTL 
			AND tin.idLT = loaitin.idLT
			AND tin.AnHien=1 AND tin.TinNoiBat=0
			AND ( tin.lang='$lang' or '$lang'='' )
			AND tin.idLT=99
			ORDER BY tin.idTin DESC
			LIMIT 0,3";
		$kq = mysql_query($sql) or die (mysql_error());	
		return $kq;
	}//end function TourMoiNhat
	
	public function TinNgoaiNuoc($lang='vi'){
		$sql="	SELECT idTin, TieuDe, TomTat, tin.Content, urlHinh, Ngay, SoLanXem, tin.AnHien, tin.TinNoiBat, TenTL, Ten  
			FROM  tin, theloai, loaitin
			WHERE tin.idTL=theloai.idTL 
			AND tin.idLT = loaitin.idLT
			AND tin.AnHien=1 AND tin.TinNoiBat=0
			AND ( tin.lang='$lang' or '$lang'='' )
			AND tin.idLT=100
			ORDER BY tin.idTin DESC
			LIMIT 0,4";
		$kq = mysql_query($sql) or die (mysql_error());	
		return $kq;
	}//end function TourMoiNhat
	
	public function TinThaiLan($lang='vi'){
		$sql="	SELECT idTin, TieuDe, TomTat, tin.Content, urlHinh, Ngay, SoLanXem, tin.AnHien, tin.TinNoiBat, TenTL, Ten  
			FROM  tin, theloai, loaitin
			WHERE tin.idTL=theloai.idTL 
			AND tin.idLT = loaitin.idLT
			AND tin.AnHien=1 AND tin.TinNoiBat=0
			AND ( tin.lang='$lang' or '$lang'='' )
			AND tin.idLT=101
			ORDER BY tin.idTin DESC
			LIMIT 0,4";
		$kq = mysql_query($sql) or die (mysql_error());	
		return $kq;
	}//end function TourMoiNhat
	
	public function NhieuTinNgoaiLe($lang='vi'){
		$sql="	SELECT TieuDe, TomTat, Content, urlHinh, Ngay, SoLanXem, AnHien, TinNoiBat 
			FROM  tin
			WHERE AnHien=1 
			AND ( lang='$lang' or '$lang'='' )
			ORDER BY Ngay DESC
			LIMIT 2,6";
		$kq = mysql_query($sql) or die (mysql_error());	
		return $kq;
	}//end function NhieuTinNgoaiLe
	//---------------------------------------TIN NGOAI LE---------------------------------------//
	
	//---------------------------------------TIN TRONG NUOC---------------------------------------//
	public function TinTrongNuoc2($lang = 'vi', $sotin = 8){		
		$sql="SELECT idTinTrongNuoc, TieuDe, TomTat, urlHinh, Ngay, SoLanXem
			FROM tintrongnuoc WHERE AnHien = 1 and ( lang = '$lang' or '$lang' = '' ) 
			ORDER BY SoLanXem DESC 	LIMIT 0,$sotin";
		$kq = mysql_query($sql) or die (mysql_error());		
		return $kq;		
	}//end function TinXemNhieu
	
	function ChiTietTinTrongNuoc($idTinTrongNuoc)  {
		settype($idTinTrongNuoc, "int");
		$sql="SELECT idTinTrongNuoc, TieuDe, TomTat, Ngay, urlHinh, Content, SoLanXem 
			FROM  tintrongnuoc
			WHERE idTinTrongNuoc=$idTinTrongNuoc AND AnHien = 1";
		$kq = mysql_query($sql) or die(mysql_error());
		return $kq;
	}//end function ChiTietTin
	
	function TinCuCungLoaiTrongNuoc($idTin, $lang ='vi', $sotin = 5){		
		settype($idTin, "int");
		$sql="SELECT idTinTrongNuoc, TieuDe, TomTat, urlHinh, Ngay, SoLanXem  FROM  tintrongnuoc 
			WHERE AnHien = 1 AND idTinTrongNuoc<$idTin AND ( lang ='$lang' or '$lang'='' )
			ORDER BY idTinTrongNuoc DESC  LIMIT 0, $sotin";		
		$kq = mysql_query($sql) or die(mysql_error());
		return $kq;		
	}//end function TinCuCungLoai
	
	//---------------------------------------TIN TRONG NUOC---------------------------------------//
	
	//---------------------------------------SUPPORT---------------------------------------//
	public function YahooStatus($idYahoo){
		$sql="	SELECT AnHien 
			FROM  yahoo
			WHERE (idYahoo=$idYahoo OR $idYahoo=-1) ";
		$kq = mysql_query($sql) or die (mysql_error());	
		return $kq;
	}//end function YahooStatus
	//---------------------------------------SUPPORT---------------------------------------//
	
	//---------------------------------------TIN TUC---------------------------------------//
	public function TinTucVanHoa($lang='vi'){
		$sql="	SELECT idTin, TieuDe, TomTat, tin.Content, urlHinh, Ngay, SoLanXem, tin.AnHien, tin.TinNoiBat, TenTL, Ten  
			FROM  tin, theloai, loaitin
			WHERE tin.idTL=theloai.idTL 
			AND tin.idLT = loaitin.idLT
			AND tin.AnHien=1 AND tin.TinNoiBat=0
			AND ( tin.lang='$lang' or '$lang'='' )
			AND tin.idLT=106
			ORDER BY tin.idTin DESC
			LIMIT 0,3";
		$kq = mysql_query($sql) or die (mysql_error());	
		return $kq;
	}//end function TinTucVanHoa
	
	public function TinTucMoi($lang='vi'){
		$sql="	SELECT idTin, TieuDe, TomTat, tin.Content, urlHinh, Ngay, SoLanXem, tin.AnHien, tin.TinNoiBat, TenTL, Ten  
			FROM  tin, theloai, loaitin
			WHERE tin.idTL=theloai.idTL 
			AND tin.idLT = loaitin.idLT
			AND tin.AnHien=1 AND tin.TinNoiBat=0
			AND ( tin.lang='$lang' or '$lang'='' )
			AND tin.idLT=104
			ORDER BY tin.idTin DESC
			LIMIT 0,3";
		$kq = mysql_query($sql) or die (mysql_error());	
		return $kq;
	}//end function TinTucMoi
	
	public function TinTucAmThuc($lang='vi'){
		$sql="	SELECT idTin, TieuDe, TomTat, tin.Content, urlHinh, Ngay, SoLanXem, tin.AnHien, tin.TinNoiBat, TenTL, Ten  
			FROM  tin, theloai, loaitin
			WHERE tin.idTL=theloai.idTL 
			AND tin.idLT = loaitin.idLT
			AND tin.AnHien=1 AND tin.TinNoiBat=0
			AND ( tin.lang='$lang' or '$lang'='' )
			AND tin.idLT=105
			ORDER BY tin.idTin DESC
			LIMIT 0,2";
		$kq = mysql_query($sql) or die (mysql_error());	
		return $kq;
	}//end function TinTucAmThuc
	//---------------------------------------TIN TUC---------------------------------------//
}//end class tin
?>